/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.controller;

import com.tesam.gedeon.entity.Network;
import com.tesam.gedeon.service.NetworkService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Sistema
 */
@Controller
public class NetworkController {
    
    @Autowired
    SystemUserController system ;
    @Autowired 
    NetworkService networkService ;
    
    @RequestMapping(value = "/mediationnetwork/index")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationnetwork/index");
        
        system.loadDataSystem(mv, request);
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationnetwork/view")
    public ModelAndView view(@ModelAttribute("Network")Network network ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationnetwork/view");
        
        system.loadDataSystem(mv, request);
        List<Network> result = networkService.getNetworkById(network);
        
        System.out.println(result.size());
        
        mv.addObject("network", result);
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationnetwork/delete")
    public ModelAndView delete(@ModelAttribute("Network")Network network , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationnetwork/delete");
        
        networkService.deleteNetwork(network);
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationnetwork/new")
    public ModelAndView newNetwork(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationnetwork/new");
        
        system.loadDataSystem(mv, request);
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationnetwork/newsave")
    public ModelAndView saveNetwork(@ModelAttribute("Network")Network network , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationnetwork/save");
        
        system.loadDataSystem(mv, request);
        int id = networkService.saveNetwork(network);
        mv.addObject("id", Integer.toString(id));
        
        return mv ;
    }
    
    @RequestMapping(value = "/mediationnetwork/update")
    public ModelAndView update(@ModelAttribute("Network")Network network ,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationnetwork/update");
        
        system.loadDataSystem(mv, request);
        
        mv.addObject("network", networkService.getNetworkById(network));
        
        return mv;
    }
    
    @RequestMapping(value = "/mediationnetwork/save")
    public ModelAndView saveupdate(@ModelAttribute("Network")Network network , HttpServletRequest request){
        ModelAndView mv = new ModelAndView("/mediationnetwork/view");
        
         system.loadDataSystem(mv, request);
         
          mv.addObject("network", networkService.updateNetwork(network));
         
        return mv ;
    }
    
     @RequestMapping(value = "/mediationnetwork/ajax" , method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxGet() {

        int cantidadRegistros = 0;
        try { 
            List<Network> list = networkService.getAllByLimit("description", "asc", cantidadRegistros, cantidadRegistros);
            // ArrayList<Cliente> list = clienteService.getAllClientes();
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();

            if (list.size() > 0) {
                for (Network network : list) {
                    JSONArray arrayPut = new JSONArray(); 
                    arrayPut.put(network.getCode()); 
                    arrayPut.put(network.getDescription());  
                    array.put(arrayPut);
                    // cantidadRegistros++;
                }
            }


            cantidadRegistros = networkService.getCountAll();
            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.print("Error - Controller-doAjax:" + e.getMessage());
        }

        return null;
    }
     
      @RequestMapping(value = "/mediationnetwork/ajax", method = RequestMethod.POST)
    public @ResponseBody
    String doAjaxPost(HttpServletRequest request) {

        List<Network> listNetwork = null;
        int cantidadRegistros = 0;

        String[] columns = {"code", "description" , "description" };

        String startLimit = request.getParameter("iDisplayStart");
        String endLimit = request.getParameter("iDisplayLength");
        String dataToSort = columns[Integer.valueOf(request
                .getParameter("iSortCol_0"))];
        String directionOfOrder = request.getParameter("sSortDir_0");
        String textSearch = request.getParameter("sSearch").toUpperCase();

        int _startLimit = 0;
        int _endLimit = 0;

        if (startLimit != null && !startLimit.equals("0")) {

            if (!startLimit.trim().isEmpty()) {
                _startLimit = Integer.valueOf(startLimit);
            }
        }

        if (endLimit != null && !endLimit.equals("0")) {
            if (!endLimit.trim().isEmpty()) {
                _endLimit = Integer.valueOf(endLimit);
            }
        }

        if (textSearch != null) {
            if (!textSearch.trim().isEmpty()) {
                textSearch = textSearch.trim();
                Network network = new Network();
                network.setDescription(textSearch);
                listNetwork = networkService.getSearchNetworkByLimit("description" , "asc" ,_startLimit,_endLimit, network);
                cantidadRegistros = networkService.getTotalBySearch(network);
                return networkService.getJsonDataFromArrayList(listNetwork, cantidadRegistros);

            }
        }

        listNetwork = networkService.getAllByLimit( dataToSort, directionOfOrder ,_startLimit, _endLimit  );

        cantidadRegistros = networkService.getCountAll();

        return networkService.getJsonDataFromArrayList(listNetwork, cantidadRegistros);
    }

    
}
